package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.view.wheelview.NumericWheelAdapter;
import com.enqualcomm.kids.view.wheelview.OnWheelChangedListener;
import com.enqualcomm.kids.view.wheelview.WheelView;

import java.util.Arrays;
import java.util.List;

import common.utils.DensityUtil;

/**
 * 主页面，播放轨迹选择日期对话框
 * @author yzq
 *
 */
public class MyDateTimePickerDialog2 extends Dialog implements
		View.OnClickListener{
	private static int START_YEAR = 2010,END_YEAR=2020;
	private final OnDateTimeSetListener mCallBack;
//	private final Calendar mCalendar;
	private int curr_year, curr_month, curr_day, curr_hour, curr_minute;
	// 添加大小月月份并将其转换为list,方便之后的判断
	String[] months_big = { "1", "3", "5", "7", "8", "10", "12" };
	String[] months_little = { "4", "6", "9", "11" };
	final WheelView wv_year, wv_month, wv_day, wv_hours, wv_mins;
	final List<String> list_big, list_little;
	private TextView tv;//修改完时间设置到这个textview
	private int type;//1:开始时间 2：结束时间,3:远程开机时间

	private int windowWidth;
	public MyDateTimePickerDialog2(Context context, OnDateTimeSetListener callBack, String dateStr, int type, TextView tv) {
		this(context, START_YEAR,END_YEAR,callBack, dateStr,type,tv);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window dialogWindow = getWindow();
		dialogWindow.setGravity(Gravity.CENTER);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		lp.width =windowWidth - DensityUtil.dip2px(getContext(), 30); // 宽度
		dialogWindow.setAttributes(lp);
		getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		
	}
	
	public MyDateTimePickerDialog2(Context context, final int START_YEAR,
								   final int END_YEAR, OnDateTimeSetListener callBack, String dateStr, int type, TextView tv) {
		super(context);
		mCallBack = callBack;
		this.tv = tv;
		this.type = type;

		list_big = Arrays.asList(months_big);
		list_little = Arrays.asList(months_little);
		// 找到dialog的布局文件
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.time_layout2, null);
		
		view.findViewById(R.id.sureBtn).setOnClickListener(this);
		view.findViewById(R.id.cancelBtn).setOnClickListener(this);
		
		TextView titleText = (TextView) view.findViewById(R.id.titleText);
		if (type ==1 ) {
			titleText.setText(getContext().getString(R.string.start_time));
		}else if (type ==2 ){
			titleText.setText(getContext().getString(R.string.end_time));
		}else if(type==3){
            titleText.setText(getContext().getString(R.string.turn_on_time));
        }else{
			titleText.setText(context.getString(R.string.remind_edit_time_title));
		}

		DisplayMetrics dm = new DisplayMetrics();
		getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
		windowWidth = dm.widthPixels;
		int textSize = DensityUtil.adjustFontSize(windowWidth);
		
		//20141202235900
		// 从dateStr解析出日期
		int year = Integer.parseInt(dateStr.substring(0, 4));
		int month =Integer.parseInt(dateStr.substring(4, 6));
		int day =Integer.parseInt(dateStr.substring(6, 8));
		int hour = Integer.parseInt(dateStr.substring(8, 10));
		int minute = Integer.parseInt(dateStr.substring(10, 12));
		
		// 年
		wv_year = (WheelView) view.findViewById(R.id.year);
		wv_year.setAdapter(new NumericWheelAdapter(START_YEAR, END_YEAR));// 设置"年"的显示数据
		wv_year.setCyclic(true);// 可循环滚动
		wv_year.setLabel(getContext().getString(R.string.year));// 添加文字
		wv_year.setCurrentItem(year - START_YEAR);// 初始化时显示的数据

		// 月
		wv_month = (WheelView) view.findViewById(R.id.month);
		wv_month.setAdapter(new NumericWheelAdapter(1, 12));
		wv_month.setCyclic(true);
		wv_month.setLabel(getContext().getString(R.string.month));
		wv_month.setCurrentItem(month-1);

		// 日
		wv_day = (WheelView) view.findViewById(R.id.day);
		wv_day.setCyclic(true);
		// 判断大小月及是否闰年,用来确定"日"的数据
		if (list_big.contains(String.valueOf(month))) {
			wv_day.setAdapter(new NumericWheelAdapter(1, 31));
		} else if (list_little.contains(String.valueOf(month))) {
			wv_day.setAdapter(new NumericWheelAdapter(1, 30));
		} else {
			// 闰年
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
				wv_day.setAdapter(new NumericWheelAdapter(1, 29));
			else
				wv_day.setAdapter(new NumericWheelAdapter(1, 28));
		}
		wv_day.setLabel(getContext().getString(R.string.day));
		wv_day.setCurrentItem(day - 1);
		
		//时
		wv_hours = (WheelView) view.findViewById(R.id.hour);
		wv_hours.setAdapter(new NumericWheelAdapter(0, 23));
		wv_hours.setCyclic(true);
		wv_hours.setLabel(getContext().getString(R.string.hour));
		wv_hours.setCurrentItem(hour);
		
		//分
		wv_mins = (WheelView) view.findViewById(R.id.minute);
		wv_mins.setAdapter(new NumericWheelAdapter(0, 59));
		wv_mins.setCyclic(true);
		wv_mins.setLabel(getContext().getString(R.string.minute));
		wv_mins.setCurrentItem(minute);
		
		wv_day.setTextSizeAndItemCount(textSize,3);
		wv_month.setTextSizeAndItemCount(textSize,3);
		wv_year.setTextSizeAndItemCount(textSize,3);
		wv_hours.setTextSizeAndItemCount(textSize,3);
		wv_mins.setTextSizeAndItemCount(textSize,3);
		
		// 添加"年"监听
		OnWheelChangedListener wheelListener_year = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				int year_num = newValue + START_YEAR;
				// 判断大小月及是否闰年,用来确定"日"的数据
				if (list_big.contains(String.valueOf(wv_month.getCurrentItem() + 1))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 31));
				} else if (list_little.contains(String.valueOf(wv_month.getCurrentItem() + 1))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 30));
				} else {
					if ((year_num % 4 == 0 && year_num % 100 != 0)
							|| year_num % 400 == 0)
						wv_day.setAdapter(new NumericWheelAdapter(1, 29));
					else
						wv_day.setAdapter(new NumericWheelAdapter(1, 28));
				}
			}
		};
		// 添加"月"监听
		OnWheelChangedListener wheelListener_month = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				int month_num = newValue + 1;
				// 判断大小月及是否闰年,用来确定"日"的数据
				if (list_big.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 31));
				} else if (list_little.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 30));
				} else {
					if (((wv_year.getCurrentItem() + START_YEAR) % 4 == 0 && (wv_year
							.getCurrentItem() + START_YEAR) % 100 != 0)
							|| (wv_year.getCurrentItem() + START_YEAR) % 400 == 0)
						wv_day.setAdapter(new NumericWheelAdapter(1, 29));
					else
						wv_day.setAdapter(new NumericWheelAdapter(1, 28));
				}
			}
		};
		wv_year.addChangingListener(wheelListener_year);
		wv_month.addChangingListener(wheelListener_month);
		
		
		setContentView(view);
	}
	public void onClick(View v) {
		if (v.getId() == R.id.cancelBtn) {
			dismiss();
		}else {

				curr_year = wv_year.getCurrentItem() + START_YEAR;
				curr_month = wv_month.getCurrentItem() + 1;
				curr_day = wv_day.getCurrentItem() + 1;
				curr_hour = wv_hours.getCurrentItem();
				curr_minute = wv_mins.getCurrentItem();
				
				/**
				 * 拼接发送给服务器的starttime stoptime
				 */
				StringBuilder builder = new StringBuilder();
				/**
				 * 拼接界面TextView显示的字符串
				 */
				StringBuilder builder2 = new StringBuilder();
				builder.append(curr_year);
				builder2.append(curr_year).append("-");
				if (curr_month < 10) {
					builder.append(0);
					builder2.append(0);
				} 
				builder.append(curr_month);
				builder2.append(curr_month).append("-");
				
				if (curr_day < 10) {
					builder.append(0);
					builder2.append(0);
				}
				builder.append(curr_day);
				builder2.append(curr_day).append(" ");
				
				if (curr_hour < 10) {
					builder.append(0);
					builder2.append(0);
				}
				builder.append(curr_hour);
				builder2.append(curr_hour).append(":");
				
				if (curr_minute < 10) {
					builder.append(0);
					builder2.append(0);
				}
				builder.append(curr_minute);
				builder2.append(curr_minute);
				
				if (type==1) {
					builder.append("00");
				}else {
					builder.append("59");
				}
			if (mCallBack != null) {
				mCallBack.onDateTimeSet(builder.toString());
			}
//				MyLogger.jLog().i(builder.toString());
				tv.setText(builder2.toString());
			dismiss();
		}
		
	}
	public  interface OnDateTimeSetListener {
			void onDateTimeSet(String resultDateStr);
	}

}

